/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.transform_t2d;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Resolve system calls (system fields).
 * <ol>For now it resolve one property:
 *  <li>date - current date</li>
 * </ol>
 * The rest properties it taken from input properties.
 * @author yura
 */
public class SystemConfiguration {

    /**
     * <pre>
     *    Pattern: \$system::(.+)  // 1st group is the name of variable
     * Human read: $system::.+
     *    Example: $system::date
     * </pre>
     */
    public static Pattern GENERAL = Pattern.compile("\\$system::(.+)");
    
    /**
     * <pre>
     *    Pattern: \$system::(.+)\((.+)\)   // 1st group is the name of variable
     *                                      // 2nd group is the arguments
     * Human read: $system::.+(.+)
     *    Example: $system::date(yy/dd//mm)
     * </pre>
     */
    public static Pattern WITH_ARGUMENT = Pattern.compile("\\$system::(.+)\\((.+)\\)");

    /** page */
    public static String PAGE_PROPERTY = "page";
    /** date */
    public static String DATE_PROPERTY = "date";
    
    /** Strings properties, that resolving not in this class */
    private Properties properties;

    /**
     * @param properties can't be null !
     */
    public SystemConfiguration(Properties properties) {
        this.properties = properties;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setProperty(String key, String value) {
        properties.setProperty(key, value);
    }

    public void setPage(int page){
        setProperty(SystemConfiguration.PAGE_PROPERTY, String.valueOf(page));
    }

    /**
     * Determine if the text is a system call
     * @param text text (for example text in variable)
     * @return true if text is a system call
     */
    public boolean isSystemRequest(String text){
        Field parse = parse(text);
        return parse != null;
    }

    /**
     * <ol>resolve properties:
     *  <li>date - current date</li>
     * </ol>
     * If text can not be resolved, it taken from input properties. If the
     * input properties don't have such property, it will be fire the exception.
     * @param text <p>input text that represent of system call. <br>
     * For example:<pre>
     *   $system::date(yy/dd//mm)   return: 11/05/06
     *   $system::date              return: Sat Nov 05 14:07:43 EET 2011
     * </pre>
     * If input properties will be contain key='page' with value='1', at the
     * moment of call this method. <pre>
     *   $system::page                return: 1
     * </pre></p>
     * @return value of propery
     * @throws IllegalArgumentException if input text is not a call of system
     * properties or if it unknown property
     */
    public String resolveSystemRequest(String text) 
            throws IllegalArgumentException{

        Field parse = parse(text);
        
        if (parse == null){
            throw new IllegalArgumentException("Wrong call of system properties: " + text);
        }
        

        if (DATE_PROPERTY.equals(parse.name)) {
            if (parse.argument != null){
                return new SimpleDateFormat(parse.argument).format(new Date());
            } else {
                return new Date().toString();
            }
        } else if (PAGE_PROPERTY.equals(parse.name)) {
            return properties.getProperty(PAGE_PROPERTY);
        } else {
            throw new IllegalArgumentException("Unknown property: " + parse.name);
        }
    }

    public static Field parse(String text){
        Matcher match = GENERAL.matcher(text);
        // check for existence of variable
        if(match.find()){
            // check for existence of arguments
            Matcher match2 = WITH_ARGUMENT.matcher(text);
            if(match2.find()){
                return new Field(match2.group(1), match2.group(2));
            } else {
                return new Field(match.group(1), null);
            }
        } else {
            return null;
        }
    }

    public static class Field {
        public final String name;
        public final String argument;

        public Field(String name, String argument) {
            this.name = name;
            this.argument = argument;
        }

        @Override
        public String toString() {
            return "Field{" + "name=" + name + "; argument=" + argument + '}';
        }
    }

}
